package com.eeprint.system.service.tenant.impl;



import com.eeprint.common.utils.IDUtil;
import com.eeprint.system.domain.platform.FORM.PlatformUserUpdateFORM;
import com.eeprint.system.domain.tenant.TenantUser;
import com.eeprint.system.domain.tenant.form.TenantUserUpdate;
import com.eeprint.system.mapper.tenant.TenantUserMapper;
import com.eeprint.system.service.tenant.TenantUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: wsen on 2020/10/18 16:13
 * @Description:
 */
@Service
public class TenantUserServiceImpl implements TenantUserService {
    @Autowired
    TenantUserMapper tenantUserMapper;

    @Override
    public TenantUser checkUser(String userPhone, String userPassword) {
        TenantUser tenantUser = tenantUserMapper.selectByUserPhone(userPhone);
        if(tenantUser != null && tenantUser.getUserPassword().equals(userPassword)){
            return tenantUser;
        }
        return null;
    }


    /**
      * @Author newhonor
      * @Date 2020/10/19
      * @Version 1.0
    **/
    @Override
    public boolean addTenantUser(TenantUser tenantUser) {

        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();

        HttpSession session = request.getSession();

        //BigInteger adminId = BigInteger.valueOf(session.getAttribute("adminId"));
        BigInteger adminId = BigInteger.valueOf(1);

        tenantUser.setId(IDUtil.getId());

        tenantUser.setCreateAt(new Date());
        tenantUser.setCreateUserId(adminId);//测试
        tenantUser.setUpdateAt(new Date());
        tenantUser.setUpdateUserId(adminId);//测试

        tenantUser.setRelTenantId(adminId);

        tenantUser.setUserRole(0);
        tenantUser.setUserLogo("user-logo");

        tenantUserMapper.addTenantUser(tenantUser);
        return true;

    }

    @Override
    public BigInteger findTenantUserIdByPhonePassword(TenantUser tenantUser) {
        return tenantUserMapper.findTenantUserIdByPhonePassword(tenantUser);
    }

    @Override
    public boolean updateTenantUser(TenantUser tenantUser) {

        BigInteger id =findTenantUserIdByPhonePassword(tenantUser);
        System.out.println(id);
        if(id==null) return false;
        if(!tenantUser.getId().equals(id)) return false;

        tenantUserMapper.updateTenantUser(tenantUser);

        return true;
    }

    @Override
    public int countTenantUserByTenantId(BigInteger tenantId) {
        return tenantUserMapper.countTenantUser(tenantId);
    }

    @Override
    public List<TenantUser> findTenantUsersByTenantId(BigInteger tenantId, int page,int limit) {
        if(page<=0||page>countTenantUserByTenantId(tenantId)) return null;
        int index = (page-1)*limit;
        return tenantUserMapper.findTenantUsers(tenantId, index,limit);
    }

    @Override
    public boolean updateTenantUserPassword(TenantUserUpdate tenantUserUpdate) {
        TenantUser tenantUser = new TenantUser();
        tenantUser.setId(tenantUserUpdate.getId());

        String newPassword = tenantUserUpdate.getNewPassword();
        String oldPassword = tenantUserUpdate.getOldPassword();
        String checkPassword = tenantUserUpdate.getCheckPassword();

        if(!newPassword.equals(checkPassword)) return false;

        TenantUser tenantUserCurrent = findTenantUserById(tenantUserUpdate.getId());

        if(tenantUserCurrent==null) return false;

        if(!tenantUserCurrent.getUserPassword().equals(oldPassword)) return false;
        tenantUser.setUserPassword(newPassword);
        tenantUserMapper.updateTenantUserPassword(tenantUser);
        return true;
    }

    @Override
    public TenantUser findTenantUserById(BigInteger id) {

        return tenantUserMapper.findTenantUserById(id);
    }

}
